package com.hbnu.controller;

import com.hbnu.dao.User;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;


@Controller   //@RestController 注解可以代替@Controller注解，但是这个注解式 Controller和ResponseBody的结合 返回的是数据
public class HomeController {
    /**
     * 自定义get请求时间格式
     */
//    @InitBinder
//    public void initBinder(ServletRequestDataBinder date){
//        date.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
//    }
    @RequestMapping("/pages/home.do")
    public String home() {
        return "home";
    }

    @RequestMapping("/param1.do")
    @ResponseBody     //此注解表示此请求不在是返回一个页面，而是返回数据
    public void param1(String name, Integer age) {
        System.out.println(name + "," + age);
    }

    @RequestMapping("/param2.do")
    @ResponseBody
    public void param2(User user) {
        //get请求格式：    ?id=19&name=jake&address=黄石市&salary=8000.00
        System.out.println(user);
    }

    @RequestMapping("/param3.do")
    @ResponseBody
    public void param3(Date date) {
        //?date=2024/4/11
        // 自定义时间格式 ?date=2024-4-11
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(date);
        System.out.println(sdf.format(date));
    }

    /**
     * 请求转发
     *
     * @return
     */
    @RequestMapping("/forward.do")
    public String forward() {
        System.out.println("this is forward....");
        return "forward:pages/home.do";

    }

    /**
     * 从定向
     *
     * @return
     */
    @RequestMapping("/redirect.do")
    public String redirect() {
        System.out.println("this is redirect......");
        return "redirect:pages/home.do";
    }
    /**
     * 乱码问题
     */

    @RequestMapping("/characterEncoding")
    @ResponseBody
    public String characterEncoding(){
        return "大家好，我叫Cosmos";
    }
}


